<?php

session_start();

require_once "Controllers/RegisterController.php";
require_once "Controllers/LoginController.php";
require_once "Controllers/PostController.php";

require_once "LoginHandler.php";
require_once "LoginView.php";
require_once "PageView.php";
require_once "PostView.php";

require_once "DBConfig.php";
require_once "Database.php";

class MasterController {
	
	public function MasterDoControll() {
				
			$db = new Database;	
			$db->Connect(new DBConfig);
			$lc = new LoginController();
			$lh = new LoginHandler($db);
			$lv = new LoginView();
			$pv = new PageView();
			$postv = new PostView();
			$pc = new PostController();
			$rc = new RegisterController();
			
			$html = "";
			
			$loginNav = "Login";
			$regNav = "Register";
			$postNav = "Post";
		
		//Om användare tryckt på register så visas registrera-sidan
		if(isset($_GET[$regNav]))
		{
			$body = $rc->RegdoControll($db);
			if($lh->IsLoggedIn() )
			{
				$header = $pv->RegisterHeader();
				$header .= $pv->LoggedInAs();
			}
			else {
				
				$header = $pv->RestrictedRegisterHeader();
			}
		}
		else if(isset($_GET[$postNav])){
			
			$body = $pc->PostDoControll($db);
			if($lh->IsLoggedIn() )
			{
			
				$header = $pv->PostHeader();
				$header .= $pv->LoggedInAs();
			}
			else {
				$header = $pv->RestrictedLoginHeader();
			
			}
		}
		//annars visas loginsidan
		else {
			$body = $lc->LogindoControll($db);
			
			if($lh->IsLoggedIn() )
			{
				$header = $pv->LoginHeader();
				$header .= $pv->LoggedInAs();
			}
			else {
				$header = $pv->RestrictedLoginHeader();
				$header .= '';
			}
		}
		
		$db->Close();
		return $pv->Page($title, $header, $body) . $html;	
	}
}

?>